/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vita;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 *
 * @author chrisf
 */
public class ImagePanel extends JPanel {

  private Image img;

  public ImagePanel () {
      img=null;
      this.setBackground(Color.white);
      this.setVisible(true);
  }

  public void setImage(String img) {
    setImage(new ImageIcon(img).getImage());
  }

  public void setImage(Image img) {
    int width = this.getWidth();
    int height = this.getHeight();
    int imgWidth = img.getWidth(null);
    int imgHeight = img.getHeight(null);
    int scaledImgWidth=0;
    int scaledImgHeight=0;
    if (imgHeight>imgWidth) {
        scaledImgHeight = height;
        scaledImgWidth = (int) (height * (double)imgWidth/(double)imgHeight);
    } else {
        scaledImgWidth = width;
        scaledImgHeight = (int) (width * (double)imgHeight/(double)imgWidth);
    }
    //int height = (int) (((double) img.getHeight(null) / img.getWidth(null)) * width);
    this.img = img.getScaledInstance(scaledImgWidth, scaledImgHeight, Image.SCALE_SMOOTH);
  }

  @Override
  public void paintComponent(Graphics g) {
    g.setColor(getBackground());
    g.fillRect(0,0,getWidth(),getHeight());
    g.drawImage(img, 0, 0, this);
  }

}
